
import { Utils } from "./utils"; 
export default {
  install(Vue, options) {
    Vue.prototype.$formatDate = function (date, formatt) {
      let fmt = formatt ? formatt : "yyyy-MM-dd HH:mm:ss";
      var o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "H+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        S: date.getMilliseconds(), //毫秒
      };
      if (/(y+)/.test(fmt))
        fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length == 1
              ? o[k]
              : ("00" + o[k]).substr(("" + o[k]).length)
          );
      return fmt;
    };

    Vue.prototype.$dealMsg = dealMsg;
    Vue.prototype.$dealMsgLS = dealMsgLS;
    Vue.prototype.$randomFloat = randomFloat;
    function dealMsg(k, data) {
      const sm4 = require("sm-crypto").sm4;
      const sms = Utils.sms;
      let sm4Encrypt = "";
      if (data != null && data != undefined) {
        var str = data;
        if (typeof data == "object") {
          str = JSON.stringify(data);
        } else if (typeof data == "boolean") {
          str = data ? "true" : "false";
        } else if (typeof data == "number") {
          str = data.toString();
        }
        sm4Encrypt = sm4.encrypt(str, sms);
        sessionStorage.setItem(k, sm4Encrypt);
      } else {
        if (!sessionStorage.getItem(k)) {
          return null;
        }
        var res;
        sm4Encrypt = sessionStorage.getItem(k) || "";
        try {
          res = JSON.parse(sm4Encrypt);
        } catch (err) {
          let sm4Decrypt = sm4.decrypt(sm4Encrypt, sms) || "{}";
          res = sm4Decrypt;
        }
        return res;
      }
    }

    function dealMsgLS(k, data) {
      const sm4 = require("sm-crypto").sm4;
      const sms = Utils.sms;
      let sm4Encrypt = "";
      if (data != null && data != undefined) {
        var str = data;
        if (typeof data == "object") {
          str = JSON.stringify(data);
        } else if (typeof data == "boolean") {
          str = data ? "true" : "false";
        } else if (typeof data == "number") {
          str = data.toString();
        }
        sm4Encrypt = sm4.encrypt(str, sms);
        localStorage.setItem(k, sm4Encrypt);

        // console.log('dealMsgLS=='+k)
        // console.log(data)
      } else {
        if (!localStorage.getItem(k)) {
          return null;
        }
        var res;
        sm4Encrypt = localStorage.getItem(k) || "";
        try {
          var test = JSON.parse(sm4Encrypt);
          res = sm4Encrypt;
        } catch (err) {
          let sm4Decrypt = sm4.decrypt(sm4Encrypt, sms) || "{}";
          res = sm4Decrypt;
        }
        // console.log('dealMsgLS=='+k)
        // console.log(res)
        // console.log('====================='+k)
        return res;
      }
    }

    function randomFloat() {
      // 生成 32 位随机值
      const fooArray = new Uint32Array(1);
      // 最大值是 2^32 –1
      const maxUint32 = 0xffffffff;
      // 用最大可能的值来除
      return crypto ? crypto.getRandomValues(fooArray)[0] / maxUint32 : 0.5;
    }
  },
};
